<?php
namespace app\api\controller;

use app\common\controller\Api; 
use app\common\model\Device as DeviceModel;
use app\common\model\DeviceLog as DeviceLogModel;
use app\common\model\Brand as BrandModel;
use app\common\model\DeviceCat as DeviceCatModel;
use app\common\model\DeviceType as DeviceTypeModel;
use app\common\model\FeeTemplate as FeeTemplateModel;
use app\common\model\DeviceLogRepair as DeviceLogRepairModel;
use think\facade\Log;
use org\Curl; 

class Device extends Api
{
    /**
     * 获取设备信息
     * @return array
     */
    public function getDeviceInfo()
    {
        $platform  = input('param.platform', 0);  // 1就是h5登录（h5端和微信公众号端），2就是微信小程序登录，3是支付宝小程序，4是app，5是pc
        $cat_id  = input('param.cat_id', 0);        // 分类
        $brand_id  = input('param.brand_id', 0);      // 品牌
        $deviceId  = input('param.deviceId', 0);      // 设备编号
        
        return $this->getDeviceInfoByID($cat_id, $brand_id, $deviceId);
    } 
 
    /**
     * 获取设备信息
     * @return array
     */
    private function getDeviceInfoByID($cat_id, $brand_id, $deviceId)
    { 
        $result = [
            'status' => false,
            'data' => [],
            'msg' => '设备不存在'
        ];

        $deviceModel = new DeviceModel();
        $deviceInfo = $deviceModel->where(['cat_id' => $cat_id, 'brand_id' => $brand_id, 'sub_id' => $deviceId])->find();
        
        if ( $deviceInfo )
        {
            $brandModel = new BrandModel();
            $deviceCatModel = new DeviceCatModel(); 
            $deviceTypeModel = new DeviceTypeModel(); 
            $deviceInfo['cat_name'] = $deviceCatModel->getNameById($deviceInfo['cat_id']);
            $deviceInfo['brand_name'] = $brandModel->getNameById($deviceInfo['brand_id']);
            $deviceInfo['type_name'] = $deviceTypeModel->getNameById($deviceInfo['type_id']);
            $deviceInfo['area'] = get_area($deviceInfo['area_id']);

            $result['status'] = true;
            $result['data'] = $deviceInfo;
            $result['msg'] = '获取成功';
        }
        return $result;
    } 

    /**
     * 获取设备参数信息
     * @return array
     */
    public function getDeviceOptions()
    { 
        $feeTemplateModel = new FeeTemplateModel();
        return $feeTemplateModel->getDeviceOptions();
    } 
    
    /**
     * 获取定价
     * @return array
     */
    public function getMoney()
    {
        $cat_id = input('param.cat_id', '');
        $brand_id = input('param.brand_id', '');
        $device_Id = input('param.deviceId', '');
        $feeType = input('param.feeType', '');
        $mins = input('param.mins', '');
        
        $feeTemplateModel = new FeeTemplateModel();
        $moneyInfo = $feeTemplateModel->getMoney($cat_id, $brand_id, $device_Id, $feeType, $mins);
        return $moneyInfo;
    } 

    /**
     * 吹风机支付
     * @return array
     */
    public function blowerPay()
    {
        $platform  = input('param.platform', 1);            // 1就是h5登录（h5端和微信公众号端），2就是微信小程序登录，3是支付宝小程序，4是app，5是pc
        
        $cat_id  = input('param.cat_id', '');               // 分类
        $brand_id  = input('param.brand_id', '');           // 品牌
        $deviceId  = input('param.deviceId', '');           // 设备编号
        $channels  = input('param.channels', '');           // 通道
        $feeType  = input('param.feeType', '');             // 费用类型
        $minsName  = input('param.minsName', '');           // 时长
        $mins  = input('param.mins', '');                   // 时长
        
        // 获取设备信息
        $deviceInfo = $this->getDeviceInfoByID($cat_id, $brand_id, $deviceId);
        if($deviceInfo['data']['cat_name'] != '声波设备') {
            return error_code(10003);
        }

        if($deviceInfo['data']['brand_name'] != '紫宇') {
            return error_code(10003);
        }
        
        // 请求设备本次启动所需支付费用
        $feeTemplateModel = new FeeTemplateModel();
        $moneyInfo = $feeTemplateModel->getMoney($deviceInfo['data']['cat_id'], $deviceInfo['data']['brand_id'], $deviceId, $feeType, $minsName); 
        if(!$moneyInfo['status']) {
            return $moneyInfo;
        }

        $pay_params['user_id'] = $this->userId;
        $pay_params['cat_name'] = $deviceInfo['data']['cat_name'];
        $pay_params['brand_name'] = $deviceInfo['data']['brand_name'];
        $pay_params['type_name'] = $deviceInfo['data']['type_name'];
        $pay_params['channels'] = $channels;
        $pay_params['mins'] = $mins;
        $pay_params['money'] = $moneyInfo['data']['money'];
        $pay_params['sub_id'] = $deviceInfo['data']['sub_id'];
        $pay_params['fee_template_id'] = $moneyInfo['data']['id'];

        // 请求支付并获取设备启动参数
        $deviceLogModel = new DeviceLogModel();
        return $deviceLogModel->pay($moneyInfo['data']['name'], $this->userId, $deviceInfo['data']['id'], $deviceInfo['data']['name'], $pay_params);
    }
 
    /**
     * 吹风机声波播放
     * @return array
     */
    public function blowerPlay()
    {
        $platform  = input('param.platform', 1);            // 1就是h5登录（h5端和微信公众号端），2就是微信小程序登录，3是支付宝小程序，4是app，5是pc
        $log_id  = input('param.log_id', '');               // 设备ID
        // 请求支付并获取设备启动参数
        $deviceLogModel = new DeviceLogModel();
        $deviceLogModel->blowerPlay($log_id);

        $return_data = array(
            'status' => true,
            'msg'    => '设置成功',
            'data'   => array(
            )
        );

        return $return_data;
    }
 
    /**
     * 获取设备启动日志
     * @return array
     */
    public function getDeviceLog()
    {
        $input = array(
            'type_id'  => input('type'),
            'page'    => input('page'),
            'limit'   => input('limit'),
            'user_id' => $this->userId
        );
 
        $deviceLogModel = new DeviceLogModel();
        $data = $deviceLogModel->getDeviceLog($input);

        $return_data = array(
            'status' => true,
            'msg'    => '获取成功',
            'data'   => array(
                'list'   => $data['data'],
                'count'  => $data['count'],
                'page'   => $input['page'],
                'limit'  => $input['limit'],
                'type' => $input['type_id']
            )
        );

        return $return_data;
    }

    /**
     * 获取设备使用详情
     * @return array
     */
    public function getDeviceLogInfo()
    {
        $platform  = input('param.platform', 1);            // 1就是h5登录（h5端和微信公众号端），2就是微信小程序登录，3是支付宝小程序，4是app，5是pc
        $id  = input('param.id', '');           // 设备ID

        $deviceLogModel = new DeviceLogModel();
        return $deviceLogModel->getDeviceLogInfo($id);
    }
    
    /**
     * 添加报修单
     * @return array|mixed
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function addRepair()
    {
        // 设备日志
        if (!input("?param.log_id")) {
            return error_code(13105);
        }
  
        $post  = input('param.');
        // 图片
        $images = [];
        if (isset($post['images'])) {
            $images = $post['images'];
        }
 
        // 模板消息
        $formId = '';

        $deviceLogRepair = new DeviceLogRepairModel();
        return $deviceLogRepair->toAdd($this->userId, input('param.log_id'), $images, input('param.reason', ''), $formId);
    }

    /**
     * 获取报修单
     * @return array|mixed
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function getRepairInfo()
    {
        $result = [
            'status' => false,
            'data'   => [],
            'msg'    => ''
        ];

        // 设备日志
        if (!input("?param.log_id")) {
            return error_code(13105);
        }
        
        //更新订单状态
        $deviceLog = new DeviceLogModel();
        $device_info = $deviceLog->where(['id'=>input('param.log_id')])->find(); 
        if(!$device_info){
            return error_code(13105);
        }
        $bill_id = $device_info['bill_id']; 
        $deviceLogRepair = new DeviceLogRepairModel();
        $info    = $deviceLogRepair->getInfo($this->userId, $bill_id);
        if (!$info['status']) {
            return $info;
        }
  
        $result['data']     = $info['data']; 
        $result['status']   = true;
        return $result;
    }
}
